package com.briup.jz.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.jz.bean.Order;
import com.briup.jz.bean.extend.OrderExtend;
import com.briup.jz.service.IOrderService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(description="订单相关接口")
@Validated
@RestController
@RequestMapping("/order")
public class OrderController  {
	
	@Autowired
	private IOrderService orderService;
	
	@ApiOperation(value = "拒绝订单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("rejectOrder")
	public Message rejectOrder(Long id) {
		orderService.changeStatus(id, "已完成");
		orderService.changeRemark(id, "被拒绝");
		return MessageUtil.success("拒绝派单成功");
	}
	
	@ApiOperation(value = "取消派单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("cancelSendOrder")
	public Message cancelSendOrder(Long id) {
		orderService.changeStatus(id, "待派单");
		return MessageUtil.success("取消派单成功");
	}
	
	@ApiOperation(value = "派单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("sendOrder")
	public Message sendOrder(Long id) {
		orderService.changeStatus(id, "待接单");
		return MessageUtil.success("派单成功");
	}
	
	@ApiOperation(value = "接单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("takeOrder")
	public Message takeOrder(Long id) {
		orderService.changeStatus(id, "待服务");
		return MessageUtil.success("接单成功");
	}
	
	@ApiOperation(value = "员工服务结束")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("ServiceCompleteOrder")
	public Message completeOrder(Long id) {
		orderService.changeStatus(id, "待确认");
		return MessageUtil.success("服务完成");
	}
	
	@ApiOperation(value = "确认定单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("confirmOrder")
	public Message confirmOrder(Long id) {
		orderService.changeStatus(id, "已完成");
		return MessageUtil.success("确认订单成功");
	}
	
	
	@ApiOperation(value = "通过id查询订单详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("findById")
	public Message findById(Long id) {
		Order order = orderService.findById(id);
		return MessageUtil.success(order);
	}
	
	@ApiOperation(value = "通过id删除订单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
	@GetMapping("deleteById")
	public Message deleteById(Long id) {
		orderService.deleteById(id);
		return MessageUtil.success("删除成功");
	}
	
	@ApiOperation(value = "提交订单")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value = "主键",paramType = "form"),
        @ApiImplicitParam(name="orderTime",value = "订单时间",paramType = "form"),
        @ApiImplicitParam(name="serviceTime",value = "服务时间",paramType = "form"),
        @ApiImplicitParam(name="total",value = "费用",paramType = "form",required = true),
        @ApiImplicitParam(name="status",value = "状态",paramType = "form"),
        @ApiImplicitParam(name="remark",value = "标注",paramType = "form"),
        @ApiImplicitParam(name="customerId",value = "顾客编号",paramType = "form",required = true),
        @ApiImplicitParam(name="addressId",value = "地址编号",paramType = "form",required = true),
        @ApiImplicitParam(name="employeeId",value = "员工编号",paramType = "form")
    })
	@PostMapping("submitOrder")
	public Message submitOrder(Order order) {
		orderService.saveOrUpdate(order);
		return MessageUtil.success("提交成功");
	}

	@ApiOperation(value = "分页查询订单信息")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="page",value = "页码",paramType = "query",required = true),
    	@ApiImplicitParam(name="pageSize",value = "每页记录数",paramType = "query",required = true),
    	@ApiImplicitParam(name="beginTime",value = "开始时间",paramType = "query"),
    	@ApiImplicitParam(name="endTime",value = "结束时间",paramType = "query"),
    	@ApiImplicitParam(name="status",value = "状态",paramType = "query"),
        @ApiImplicitParam(name="customerId",value = "顾客编号",paramType = "query"),
    	@ApiImplicitParam(name="employeeId",value = "员工编号",paramType = "query")
    })
	@GetMapping("pageQuery")
	public Message pageQuery(int page, int pageSize,Long beginTime, Long endTime, String status, Long customerId, Long employeeId) {
		List<OrderExtend> orders = orderService.pageQuery(page, pageSize,beginTime,endTime, status,customerId,employeeId);
		return MessageUtil.success(orders);
	}
	
}
